package com.happy3w.ssp.schoolmatehome.component.image;

import com.happy3w.ssp.schoolmatehome.component.image.action.ColorAction;
import com.happy3w.ssp.schoolmatehome.component.image.action.FontAction;
import lombok.RequiredArgsConstructor;

import java.awt.Color;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Stream;

@RequiredArgsConstructor
public class LinePlainBuilder {
    private final LinePrintContext context;
    private final TypePrinterManager typePrinterManager;

    private List<DrawAction> actions = new ArrayList<>();

    public LinePlainBuilder printLine(Object...items) {
        Stream.of(items)
                .filter(Objects::nonNull)
                .map(item -> typePrinterManager.findPrinter(item).buildActions(item, context))
                .forEach(actions::addAll);
        context.nextLine();
        return this;
    }

    public DrawPlan build() {
        int maxHeight = actions.stream()
                .mapToInt(a -> (a instanceof  LocationDrawAction s) ? s.getLocation().y : 0)
                .max()
                .orElse(0);
        return new DrawPlan()
                .width(context.getWidth())
                .height(maxHeight)
                .font(context.getFont())
                .actions(
                        Stream.concat(
                                Stream.of(new FontAction(context.getFont()),
                                        new ColorAction(Color.black)),
                                actions.stream()
                        ).toList());
    }
}
